<template>
  <div :class="{'mobile-create': showMobilePage}" class="create-cost-dialog">
    <el-dialog
      :visible.sync="visible"
      :append-to-body="false"
      :before-close="handleCancel"
      :class="{'global-mobile-dialog': showMobilePage}"
      class="dialog-size-auto small-common-dialog-size"
      width="450px">
      <div class="main">
        <Title title="设置采集方式" font-size="16px" style="margin-bottom:20px;"/>
        <el-form
          ref="createDataForm"
          :rules="rules"
          :model="form"
          :label-position="formAlign"
          label-width="80px"
          class="lineHeight32">
          <el-form-item label="采集方式" prop="collectWay" style="margin-bottom:24px;">
            <el-select v-model="form.collectWay" placeholder="请选择" class="width240" size="small" @change="changeCollectWays">
              <el-option
                v-for="item in collectWaysOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <template v-if="form.collectWay !== 0">
            <el-form-item v-for="(item, index) in form.collectDate" :key="item._uuid" :rules="rules.time" :prop="`collectDate.${index}.time`" label="采集日期">
              <div class="flex collect-date-box">
                <el-date-picker
                  v-model="item.time"
                  :picker-options="disabledDate(form.collectWay)?pickerOptions:null"
                  :class="{'delete-picker':showMobilePage && index > 0, 'add-delete-picker':showMobilePage && index === form.collectDate.length-1}"
                  class="width240"
                  size="small"
                  format="yyyy/MM/dd"
                  value-format="timestamp"
                  placeholder="选择日期" />
                <div v-if="form.collectWay === 4" class="btns flex-center">
                  <template v-if="!showMobilePage">
                    <span v-if="index === form.collectDate.length-1" class="add-btn" @click="handleAdd">添加</span>
                    <span v-if="index > 0" class="del-btn" @click="handleDel(index)">删除</span>
                  </template>
                  <template v-else>
                    <div v-if="index > 0" class="del-btn" @click="handleDel(index)">
                      <i class="delete-icon el-icon-delete"/>
                    </div>
                  </template>
                </div>
              </div>
              <div v-if="showMobilePage && index === form.collectDate.length-1" class="add-btn-mobile" @click="handleAdd">添加<span class="add-icon">+</span></div>
            </el-form-item>
          </template>
        </el-form>
        <div class="btn-container">
          <el-button size="small" type="primary" @click="handleSubmit">确定</el-button>
          <el-button type="info" class="cancel-btn-style" size="small" @click="handleCancel">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import { standardFormat } from '@/filters/date';
import { editReportDetail } from '@/api/publish';
import setWay from '@/views/publish/module/setWay';
import mobileCreate from '@/mixins/mobileCreate';

export default {
  components: { Title },
  filters: { standardFormat },
  mixins: [setWay, mobileCreate],
  data() {
    return {
      visible: false,
      id: '',
      form: {
        collectDate: [{ time: null }],
        collectWay: ''
      },
      rules: {
        collectWay: [{ required: true, trigger: 'change', message: '请选择采集方式' }],
        time: [{ required: true, trigger: 'change', message: '请选择采集日期' }]
      }
    };
  },
  methods: {
    openDialog(id, data) {
      this.id = id;
      this.form.collectWay = data.collectWays;
      const temp = [];
      data.collectDate.forEach(item => {
        temp.push({ time: item * 1000 });
      });
      this.form.collectDate = temp;
      this.visible = true;
    },
    handleSubmit() {
      // 校验必填
      this.$refs['createDataForm'].validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          params.collectDate = params.collectDate.map(item => {
            item = item.time / 1000;
            return item;
          });
          if (params.collectWay === 0) {
            params.collectDate = [];
          }
          if (params.collectDate.length > 0) {
            // 对采集日期去重
            params.collectDate = [...new Set(params.collectDate)];
          }
          params.collectWays = params.collectWay;
          delete params.collectWay;
          editReportDetail(this.id, params).then(res => {
            this.$emit('updateData');
            this.handleCancel();
          }).catch(err => {
            console.log(err);
          });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.$refs['createDataForm'].resetFields();
      this.form.collectDate = [];
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.width240 {
  width: 240px;
}
.create-cost-dialog {
  &::v-deep {
    .el-form-item {
      margin-bottom: 22px !important;
    }
    .delete-picker {
      width: 180px !important;
    }
    .add-delete-picker {
      width: 150px !important;
    }
  }
  &.mobile-create {
    .add-delete-picker, .delete-picker {
      width: 100% !important;
    }
  }
}
.btn-container {
  display: flex;
  justify-content: center;
}
.collect-date-box {
  .btns{
    font-size: 14px;
    cursor: pointer;
    .add-btn{
      margin-left: 12px;
      color: #406EFF;
      white-space: nowrap;
    }
    .del-btn{
      margin-left: 12px;
      color: #192452;
      white-space: nowrap;
    }
  }
}
.add-btn-mobile {
  color: #406EFF;
  .add-icon {
    font-size: 18px;
    margin-left: 8px;
    font-weight: bold;
  }
}
</style>
